package com.th.supcom.pds.drugsupply.client.widget;

import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.EventType;
import com.extjs.gxt.ui.client.event.KeyListener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.toolbar.LabelToolItem;
import com.extjs.gxt.ui.client.widget.toolbar.SeparatorToolItem;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.user.client.ui.AbstractImagePrototype;
import com.th.supcom.pds.common.client.model.icon.Resources;
import com.th.supcom.pds.drugsupply.client.common.PdsSplitInfoModuleEvents;
import com.th.supcom.pds.drugsupply.client.model.PdsPackageInfoModel;

/**
 * 药品属性维护工具条
 * 
 * @author Zli
 */
public class DrugSplitPanelToolBar extends ToolBar
{
    private final CheckBoxSelectionModel <PdsPackageInfoModel> m_chkModel;
    /** 查询药品条件 */
    private TextField <String> m_phamName;

    public DrugSplitPanelToolBar (final CheckBoxSelectionModel <PdsPackageInfoModel> chkModel)
    {
        this.m_chkModel = chkModel;
        this.initButtons ();
        addListeners ();
    }

    private void addListeners ()
    {
        m_phamName.addKeyListener (new KeyListener ()
        {
            @Override
            public void componentKeyDown (final ComponentEvent event)
            {
                super.componentKeyDown (event);
                if (event.getKeyCode () == KeyCodes.KEY_ENTER)
                {
                    final AppEvent event2 = new AppEvent (PdsSplitInfoModuleEvents.QUERY_INFO);
                    Dispatcher.forwardEvent (event2);
                }
            }
        });

    }

    public TextField <String> getPhamName ()
    {

        return m_phamName;
    }

    private Button initBtn (final String tile, final EventType eventType, final CheckBoxSelectionModel <PdsPackageInfoModel> chkModel, final AbstractImagePrototype icon)
    {// 按钮监听事件
        final Button btn = new Button (tile);
        btn.setIcon (icon);
        btn.addSelectionListener (new SelectionListener <ButtonEvent> ()
        {
            @Override
            public void componentSelected (final ButtonEvent be)
            {
                final AppEvent event = new AppEvent (eventType);
                if (null != chkModel)
                {
                    event.setData (chkModel.getSelectedItems ());
                }
                Dispatcher.forwardEvent (event);
            }
        });
        return btn;
    }

    private void initButtons ()
    {
        this.add (initBtn ("新增", PdsSplitInfoModuleEvents.ADD_DRUG_INFO, m_chkModel, Resources.s_ICONS.add ()));
        this.add (new SeparatorToolItem ());
        this.add (initBtn ("删除", PdsSplitInfoModuleEvents.DELETE_DRUG_INFO, m_chkModel, Resources.s_ICONS.delete ()));
        this.add (new SeparatorToolItem ());
        this.add (initBtn ("保存", PdsSplitInfoModuleEvents.SAVE_INFO, m_chkModel, Resources.s_ICONS.save ()));
        this.add (new SeparatorToolItem ());
        this.add (initBtn ("打印", PdsSplitInfoModuleEvents.PRINT_DRUG_INFO, m_chkModel, Resources.s_ICONS.print ()));
        this.add (new SeparatorToolItem ());
        // this.add (initBtn ("刷新", PdsSplitInfoModuleEvents.REFRESH_DRUG_INFO,
        // m_chkModel, Resources.s_ICONS.refresh ()));
        final LabelToolItem lable = new LabelToolItem ("药品名称:");
        m_phamName = new TextField <String> ();
        m_phamName.setEmptyText ("请输入药品中文名称或拼音码");
        m_phamName.setWidth (200);
        lable.setStyleAttribute ("margin-left", "200px");
        // this.add (lable);
        // this.add (m_phamName);
        // this.add (initBtn ("查询", PdsSplitInfoModuleEvents.QUERY_INFO,
        // m_chkModel, Resources.s_ICONS.query ()));
    }
}
